﻿using Core.DomainServices;
using Excel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.DomainServices
{

    public sealed class ExcelRepository : IDataRepository
    {
        private  IExcelDataReader excelReader;

        public DataSet ReadData(string path)
        {
            try
            {
                string ext = Path.GetExtension(path);
                FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read);

                //1. Reading from a binary Excel file ('97-2003 format; *.xls)
                if (ext == ".xls") excelReader = ExcelReaderFactory.CreateBinaryReader(stream);

                //2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
                else if (ext == ".xlsx") excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                DataSet result = excelReader.AsDataSet();
                excelReader.IsFirstRowAsColumnNames = true;
                    excelReader.Close();
                return result;
            }
            catch(Exception ex) {

            }
            return null;
        }

        public IList<string> GetSheetNames(DataSet ds)
        {
            var items = new List<string>();
            if (ds != null && ds.Tables.Count > 0)
            { 
                foreach(DataTable item in ds.Tables)
                {
                    string TableName = item.TableName;
                    Console.WriteLine(TableName);
                    items.Add(TableName);
                }
            }
            return items;
        }

        public DataTable GetItemsBySheetName(DataSet ds, string sheetName)
        {
            if (!string.IsNullOrEmpty(sheetName)) {
                foreach(DataTable item in ds.Tables)
                {
                    if (item.TableName == sheetName)
                    {

                        return item;
                    }
                }
            }

            return null;
        }

        public void Print(DataTable dt, List<int> columnIndexs)
        {
            if (dt != null && dt.Rows.Count > 0)
            { 
                foreach(DataRow row in dt.Rows)
                {
                    Console.Write(dt.Rows.IndexOf(row));
                    foreach (var index in columnIndexs)
                    {
                        Console.Write("=>" + row[columnIndexs[index]]);
                    }
                    Console.WriteLine();
                }
            }
        }

        public void Dispose()
        {
            try
            {
                if (excelReader != null)
                {
                    excelReader.Close();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }
            catch { }
        }
    }
}
